import Component from "../../../../wxsys/lib/base/component";

function request(option) {
    return new Promise(function(resolve, reject) {
        option = option || {};
        option.success = function(res) {
            resolve(res);
        };
        option.fail = function(error) {
            reject(error);
        };
        wx.request(option);
    });
}

export default class WxRefund extends Component {
    constructor(page, id, props, context) {
        super(page, id, props, context);
    }

    sendRefund() {
        let self = this;
        let options = {};

        //微信小程序退款
        //options.payType = "miniprogram";
        let outTradeNoExpr = this.props.outTradeNoExpr;
        let outRefundNoExpr = this.props.outRefundNoExpr;
        let totalFeeExpr = this.props.totalFeeExpr;
        let refundFeeExpr = this.props.refundFeeExpr;

        // 退款前
        this.fireEvent('refundBefore', {
            source: this
        });

        if(outTradeNoExpr){
        	options.outTradeNo = this.page[this.props.$executeExprBinding_outTradeNoExpr](this.context.vars) || this.props.outTradeNoExpr;
        	if(!options.outTradeNo){
        		self.page.hint("已付款订单号不能为空！");
        		return;
        	}
        } else {
        	self.page.hint("请设置已付款订单号");
        	return;
        }
        
        if(outRefundNoExpr){
        	options.outRefundNo = this.page[this.props.$executeExprBinding_outRefundNoExpr](this.context.vars) || this.props.outRefundNoExpr;
        	if(!options.outRefundNo){
        		self.page.hint("商户退款单号不能为空！");
        		return;
        	}
        } else {
        	self.page.hint("请设置商户退款单号");
        	return;
        }

        if(totalFeeExpr){
        	this.totalFeeExpr = this.page[this.props.$executeExprBinding_totalFeeExpr](this.context.vars) || this.props.totalFeeExpr;
        	if(!this.totalFeeExpr){
        		self.page.hint("订单金额不能为空！");
        		return;
        	}else{
        		options.totalFee = parseFloat(this.totalFeeExpr)*100;
        	}
        } else {
        	self.page.hint("请设置订单金额");
        	return;
        }

        if(refundFeeExpr){
        	this.refundFeeExpr = this.page[this.props.$executeExprBinding_refundFeeExpr](this.context.vars) || this.props.refundFeeExpr;
        	if(!this.refundFeeExpr){
        		self.page.hint("退款金额不能为空！");
        		return;
        	}else{
        		options.refundFee = parseFloat(this.refundFeeExpr)*100;
        	}
        } else {
        	self.page.hint("请设置退款金额");
        	return;
        }

        request({
            url: wx.App.baseUrl + '/wxapi/miniapp/pay/refund',
            method: 'POST',
            data: JSON.stringify(options),
            header: {
                'content-type': 'application/json;charset=UTF-8'
            }
        }).then((res) => {
            var msg = res.data.msg;
            if(msg.indexOf('<err_code_des>')>0){
                msg = msg.split('<err_code_des><![CDATA[')[1].split(']]></err_code_des>')[0]
            }

        	if (res.data.status == 0 && res.data.data != undefined) {
				if (res.data.data.resultCode == "SUCCESS") {
					self.page.hint("退款成功！");
					// 退款成功事件
					self.fireEvent('refundSuccess', {
				        source: self,
				        data: res.data
				    });
				} else {
					 self.page.hint(res.data.data.errCodeDes);
					// 退款失败事件
					self.fireEvent('refundFail', {
				        source: self,
				        data: res.data
				    });
				}
			} else {
				self.page.hint(msg);
				// 退款失败事件
				self.fireEvent('refundFail', {
			        source: self
			    });
			}
        })    
    }

    initOperation() {
        super.initOperation();

        var operations = {
            'sendRefund': {
                label: "退款",
                argsDef: [],
                method: function(args) {
                    return this.owner.sendRefund();
                }
            }

        };

        for (let name in operations)
            this.defineOperation(name, operations[name]);
    }

}

wx.comp = wx.comp || {};
wx.comp.WxRefund = WxRefund;